package com.youyun56.gps.android.common;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.youyun56.gps.android.commands.CommandType;
import com.youyun56.gps.android.pojo.Car;
import com.youyun56.gps.android.pojo.SearchObject;
import com.youyun56.gps.android.service.LocationUploadService;

public class CommandFactory {
    private final static String AND     = "&";
    private static final String EQUALS  = "=";
    public static final String  COMMAND = "json";
    
    public static String uploadLocation(Location location, Long carId) {
        if (location == null)
            return null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", CommandType.UploadLocation);
        map.put("lat", location.getLat());
        map.put("lng", location.getLon());
        map.put("carid", carId);
        return getCommand(map);
    }
    
    public static String login(String carId, String pwd, String version, String imsi) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", CommandType.Login);
        map.put("carId", carId);
        map.put("pwd", pwd);
        map.put("version", version);
        map.put("imsi", imsi);
        return getCommand(map);
    }
    
    public static String getGoods(Integer cityId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", CommandType.GetGoods);
        map.put("city", cityId);
        return getCommand(map);
    }
    
    private static String getCommand(Map<String, Object> map) {
        StringBuilder sb = new StringBuilder();
        for (Entry<String, Object> entry : map.entrySet()) {
            if (sb.length() > 0) {
                sb.append(AND);
            }
            sb.append(entry.getKey());
            sb.append(EQUALS);
            sb.append(entry.getValue());
        }
        return sb.toString();
    }
    
    public static String LOGOUT() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", CommandType.USER_OUT);
        return getCommand(map);
    }
    
    public static String changeCarHearvy(String string) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", CommandType.Change_Car_Hearvy);
        map.put("hearvy", string);
        return getCommand(map);
    }
    
    public static String updateCarStatus(Car car) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", CommandType.Update_Car_Status);
        return getCommand(map);
    }
    
    public static String appendSearchGood(SearchObject so, Integer last) {
        return new StringBuilder("id=").append(CommandType.Good_Search_END).append(CommandType.AND).append(so.toString()).append(CommandType.AND).append("lastindex=").append(last).toString();
    }
    
    public static String searchGood(SearchObject so) {
        return new StringBuilder("id=").append(CommandType.Good_Search).append(CommandType.AND).append(so.toString()).toString();
    }
    
    public static String getAppendGoodsEnd(Integer cityId, Integer index) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", CommandType.Append_Good_END);
        map.put("city", cityId);
        map.put("endIndex", index);
        return getCommand(map);
    }
    
    public static String testCon() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", CommandType.Test_Connect);
        return getCommand(map);
    }
    
    public static String reConnect(Car car) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", CommandType.Re_Connect);
        map.put("carid", car.getId());
        map.put("index", car.getGoodIndex());
        if (car.getFocusCity() != null) {
            map.put("focusCity", car.getFocusCity());
        }
        return getCommand(map);
    }
    
    public static String getAppendGoods(Integer cityId, int index) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (index == -1) {
            map.put("id", CommandType.GetGoods);
        } else {
            map.put("id", CommandType.Append_Good);
        }
        map.put("city", cityId);
        map.put("index", index);
        return getCommand(map);
    }
    
    public static String getAdminInfo() {
        // TODO Auto-generated method stub
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", CommandType.Get_Admin_Info);
        map.put("carid", LocationUploadService.getInstance().getCar().getId());
        return getCommand(map);
    }
}
